package utility;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class conversion {
	/**
	 * Parse String to Date in format yyyy.MM.dd.
	 *
	 * @param  str - the string to convert
	 * @return 	null - if the string is empty or there was an error
	 * 			Date - if the conversion was successful 
	 */
	public static Date parseStringToDate(String str){
		Date date = null;
		try {
			if (str == "")
				date = null;
			else
				date = new SimpleDateFormat("yyyy.MM.dd").parse(str);
		} catch (ParseException e) {
			System.out.println("Wrong data format:" + e.getMessage());
			date = null;
		}
		return date;
	}
	/**
	 * Parse Date to String in format yyyy.MM.dd.
	 *
	 * @param  d - the date to convert
	 * @return the string in format yyyy.MM.dd
	 * 		   empty string if d is null
	 */
	public static String parseDateToString(Date d){
		if (d == null)
			return "";
		else
		return new SimpleDateFormat("yyyy.MM.dd").format(d);
	}

}
